const myAsync = (gen) => {
  return new Promise((resolve, reject) => {
      let g = gen();

      function step(val) {
          try {
              var res = g.next(val);
          } catch (e) {
              return reject(e)
          }
          if (res.done) return res.value;
          Promise.resolve(res.value).then(val => {  //防止后边是基本类型
              step(val)
          }, err => {
              g.throw(err)   //Generator.prototype.throw()将错误抛出被外层try-catch捕获
          })
      }
      step();
  })
}

function* myGenerator() {
  try {
      console.log(yield Promise.resolve(1))
      console.log(yield 2) //2
      console.log(yield Promise.reject('error'))
  } catch (error) {
      console.log(error)
  }
}

const result = myAsync(myGenerator);